using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UserSorce.Entity.Models;

namespace UserLogicCore.Logic
{
   public  class InventoryService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();
        /// <summary>
        /// 查看所有的清单
        /// </summary>
        /// <returns></returns>
        public List<Inventory> GetAllInventory(int userId,int typeId)
        {
            return _ctx.Inventory.Where(p => p.UserId == userId && p.TypeId==typeId).ToList();
        }
        /// <summary>
        ///修改清单名 
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="changeName"></param>
        /// <returns></returns>
        public void ModifyInventoryName(int userId, int typeId, int inventoryId, string changeName)
        {
            var result = FindInventory(userId, typeId, inventoryId);
            if (result != null)
            {
                result.InventoryName = changeName;
                _ctx.SaveChanges();
            }
        }
        /// <summary>
        ///创建清单,输入新建清单的名称
        /// </summary>
        /// <param name="user"></param>
        /// <param name="listID"></param>
        /// <param name="inventoryName"></param>
        /// <param name="overTime"></param>
        /// <returns></returns>
        public void AddInventory(int userId,int typeId, string inventoryName)
        {
            Inventory addList = new Inventory
            {
                InventoryName = inventoryName,
                UserId = userId,
                TypeId=typeId
            };
            _ctx.Inventory.Add(addList);
            _ctx.SaveChanges();

        }
        /// <summary>
        /// 删除清单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="listId"></param>
        public void DeleteInventory(int userId, int typeId, int inventoryId)
        {
            var inventory = FindInventory(userId, typeId,inventoryId);
            foreach (var item in inventory.Matter)
            {
                inventory.Matter.Remove(item);
            }
            _ctx.Inventory.Remove(inventory);
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 寻找清单
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public Inventory FindInventory(int userId, int typeId,int inventoryId)
        {
            return _ctx.Inventory.SingleOrDefault(prop => prop.TypeId == typeId && prop.UserId == userId &&prop.InventoryId==inventoryId);
        }
    }
}
